﻿using System;
using System.Text;

namespace ChatServerApp.Tools
{
    public static class Tools
    {
        public static String NL = System.Environment.NewLine;

        public static String USER_DATA = @"USER.XML";

        private static int server_port = 1500;

        public static int SERVER_PORT
        {
            get { return Tools.server_port; }
            set { Tools.server_port = value; }
        }

        public static byte[] ToAsciiByteArray(this string s)
        {
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetBytes(s);
        }

        public static byte[] ToByteArray(this string s)
        {
            var enc = Encoding.GetEncoding(0);
            return enc.GetBytes(s);
        }

        public static string ToAsciiString(this byte[] b)
        {
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetString(b);
        }

        public static string[] ToAsciiString(this byte[] b, string sep)
        {
            string[] array = new string[] { sep };
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetString(b).Split(array, StringSplitOptions.None);
        }

        public static string[] ToAsciiString(this string s, string sep)
        {
            string[] array = new string[] { sep };
            return s.Split(array, StringSplitOptions.None);
        }

        public static void UpdateServer()
        {
            string[] config = System.IO.File.ReadAllLines("srv.cfg");
            foreach (string element in config)
            {
                if (element.ToLower().Contains("server_port"))
                {
                    try
                    {
                        SERVER_PORT = Convert.ToInt32(element.Substring(element.IndexOf("=") + 1).Trim());
                    }
                    catch
                    {

                    }
                }
            }
        }

    }
}
